package com.example.demo2.tools;


import com.example.demo2.constant.DictConstants;

import java.math.BigDecimal;
import java.text.ParseException;

/**
 * Created by Sonya on 2017/9/11.
 * 根据舍入方式 计算金额
 */
public class RoundingTypeUtil {

    /**
     * 
     * @param base 基数
     * @param ratio 比例
     * @param num 常量
     * @param type 类型 （舍入类型 常量类） 参看DictConstants
     * @return
     */
    public static BigDecimal calc(BigDecimal base, BigDecimal ratio, BigDecimal num, Integer type){

        if(base == null) base = new BigDecimal(0);
        if(ratio == null) ratio = new BigDecimal(0);
        if(num == null) num = new BigDecimal(0);
        BigDecimal sum = num.add(base.multiply(ratio));
        if(type != null){
            if(type.equals(DictConstants.ROUNDING_SYSRDY) ){
                sum = new BigDecimal(String.valueOf(sum)).setScale(0,BigDecimal.ROUND_HALF_UP);
            }else if(type.equals(DictConstants.ROUNDING_SYSRDJ)){
                sum = new BigDecimal(String.valueOf(sum)).setScale(1, BigDecimal.ROUND_HALF_UP);
            }else if(type.equals(DictConstants.ROUNDING_SYSRDF)){
                sum = new BigDecimal(String.valueOf(sum)).setScale(2, BigDecimal.ROUND_HALF_UP);
            }else if(type.equals(DictConstants.ROUNDING_XXSRDY)){
                sum = new BigDecimal(String.valueOf(sum)).setScale(0,BigDecimal.ROUND_DOWN);
            }else if(type.equals(DictConstants.ROUNDING_XSSRDY)){
                sum = new BigDecimal(String.valueOf(sum)).setScale(0,BigDecimal.ROUND_UP);
            }else if(type.equals(DictConstants.ROUNDING_XSSRDJ)){
                sum = new BigDecimal(String.valueOf(sum)).setScale(1, BigDecimal.ROUND_UP);
            }else if(type.equals(DictConstants.ROUNDING_XSSRDF)){
                sum =new BigDecimal(String.valueOf(sum)).setScale(2, BigDecimal.ROUND_UP);
            }else if(type.equals(DictConstants.ROUNDING_XSSRDL)){
                sum = new BigDecimal(String.valueOf(sum)).setScale(3, BigDecimal.ROUND_UP);
            }
        }

        return sum;
    }

    public static void main(String args[]) throws ParseException {
        BigDecimal base = new BigDecimal(5245);
        BigDecimal radio = new BigDecimal(0.333);
        BigDecimal num = new BigDecimal(19);
        BigDecimal sum = RoundingTypeUtil.calc(base,radio,num,11);
        System.out.print(sum);
    }

}
